const filmAndCinemasModel = require('../model/filmAndCinemasModel');
const filmsModel = require('../model/filmsModel');
const cinemasModel = require('../model/cinemasModel');

// 查所有
module.exports.findAll = async function (searchObj) {
    return await filmAndCinemasModel.find(searchObj)
        .populate({ path: 'cinema' })
        .populate({ path: 'film' })
}

// 根据日期，返回指定日期的 排片内容
module.exports.findByDate = async function (date, filmId, cinemaId) {
    let filmAndCinema = await filmAndCinemasModel.find({ film: filmId, cinema: cinemaId });
    let schedules = filmAndCinema[0].schedule;
    schedules = schedules.filter(item => date == item.date);
    console.log(schedules);
    return schedules
}

// 查询并分页
// searchObj 里应该是 影院 或 电影 id
module.exports.find = async function (searchObj, page) {
    let { current, size } = page;
    let total = await filmAndCinemasModel.countDocuments(searchObj);
    let totalPage = Math.ceil(total / parseInt(size));
    let rows = await filmAndCinemasModel
        .find(searchObj)
        .populate({ path: 'cinema' })
        .populate({ path: 'film' })
        .limit(parseInt(size))
        .skip((current - 1) * size)
    return { current, size, total, totalPage, rows };
}

// 根据ID查询关联
module.exports.findById = async function (id) {
    return await filmAndCinemasModel.findById(id); // 返回对象，若找不到，返回 null;
}
// 增加关联
module.exports.insert = async function (filmAndCinemas) {
    return await filmAndCinemasModel.create(filmAndCinemas);
}
// 修改关联
module.exports.update = async function (id, filmAndCinemas) {
    return await filmAndCinemasModel.updateOne({ _id: id }, filmAndCinemas);
}
// 修改关联里的 schedule
module.exports.updateSchedule = async function (id, oldScheduleJSON, newScheduleJSON) {
    let { schedule } = await filmAndCinemasModel.findById(id);
    try {
        // console.log(schedule);
        // 如果 oldScheduleJSON 传递过来的参数是 add 那么执行新增功能     否则执行 修改、删除功能
        if (oldScheduleJSON == 'add' && newScheduleJSON) {
            schedule.unshift(JSON.parse(newScheduleJSON))
            return await filmAndCinemasModel.updateOne({ _id: id }, { schedule });

        } else {
            for (let i = 0; i < schedule.length; i++) {
                // 删除功能
                let oldSchedule = JSON.parse(oldScheduleJSON);
                if (schedule[i].date == oldSchedule.date && schedule[i].name == oldSchedule.name && schedule[i].time == oldSchedule.time && schedule[i].price == oldSchedule.price) {
                    schedule.splice(i, 1);
                    if (newScheduleJSON) {
                        // 修改功能
                        schedule.splice(i, 0, JSON.parse(newScheduleJSON))
                    }
                    return await filmAndCinemasModel.updateOne({ _id: id }, { schedule });
                }
            }
        }
    } catch (err) {
        console.log('json 文件出错');
        return {}
    }
}
// 根据ID删除关联
module.exports.delete = async function (id) {
    return await filmAndCinemasModel.deleteOne({ _id: id });
}
